package algocasts;

import java.util.ArrayList;
import java.util.List;

public class P116_SubSets {
    private void subsets(int[] nums, int start,
                         List<Integer> elem, List<List<Integer>> result) {
        result.add(new ArrayList<>(elem));
        for (int i = start; i < nums.length; i++) {
            elem.add(nums[i]);
            subsets(nums, i + 1, elem, result);
            elem.remove(elem.size() - 1);
        }
    }

    // Time: O(2^n)  Space: O(n)
    public List<List<Integer>> subsets_Recursion(int[] nums) {
        if (nums == null || nums.length == 0) return new ArrayList<>();
        List<Integer> elem = new ArrayList<>();
        List<List<Integer>> result = new ArrayList<>();
        subsets(nums, 0, elem, result);
        return result;
    }

    // Time: O(n * 2^n)  Space: (1)
    public List<List<Integer>> subsets(int[] nums) {
        if (nums == null || nums.length == 0) return new ArrayList<>();
        List<List<Integer>> result = new ArrayList<>();
        int n = nums.length;
        int N = (int) Math.pow(2, n);
        for (int i = 0; i < N; i++) {
            List<Integer> elem = new ArrayList<>();
            for (int j = 0; j < n; j++)
                if (((i >> j) & 1) == 1)
                    elem.add(nums[j]);
            result.add(elem);
        }
        return result;
    }

    public static void main(String[] args) {
        int[] nums = new int[]{1, 2, 3, 4};
        P116_SubSets solution = new P116_SubSets();
        List<List<Integer>> lists = solution.subsets_Recursion(nums);
        for (List<Integer> list : lists) {
            for (Integer integer : list) {
                System.out.print(integer + " ");
            }
            System.out.println();
        }
    }
}
